package com.hhxy.http.test;

import com.hhxy.http.mapper.UserMapper;
import com.hhxy.http.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;

public class MyBatisDemo {
    public static void main(String[] args) throws Exception {
        //1、定位xml文件，获取SqlSessionFactory对象
        String resource = "./mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2、获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        /*---------------------------------Mapper代理核心代码--------------------------------*/
        //3、执行SQL语句
        //3.1 获取UserMapper接口对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //3.2 获取SQL语句执行后的返回对象
        List<User> users = userMapper.selectAll();
        /*----------------------------------------------------------------------------------*/
        System.out.println(users);
        //5、释放资源
        sqlSession.close();
    }
}
